package com.simple.admin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.simple.framework.constant.HttpStatus;
import com.simple.framework.core.controller.BaseController;
import com.simple.framework.core.domain.R;
import com.simple.framework.core.domain.dto.PagingDto;
import com.simple.framework.core.domain.vo.PagingVo;
import com.simple.system.annotation.SystemLog;
import com.simple.system.domain.dto.SysPostDto;
import com.simple.system.domain.entity.SysPost;
import com.simple.system.enums.BusinessType;
import com.simple.system.service.SysPostService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 岗位信息管理-控制器
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys_post")
public class SysPostController extends BaseController {
    private final SysPostService sysPostService;

    public SysPostController(SysPostService sysPostService) {
        this.sysPostService = sysPostService;
    }

    /**
     * 岗位分页查询
     *
     * @param dto 查询参数
     * @return PagingVo<SysPost>
     */
    @GetMapping
    public R<PagingVo<SysPost>> list(PagingDto dto) {
        Page<SysPost> page = sysPostService.list(dto);
        return success(new PagingVo<>(page.getRecords(), page.getTotal()));
    }

    /**
     * 根据id查询岗位
     *
     * @param id 岗位id
     * @return SysPost
     */
    @GetMapping("/{id}")
    public R<SysPost> findById(@PathVariable("id") Long id) {
        return success(sysPostService.findById(id));
    }

    /**
     * 根据id删除岗位
     *
     * @param id 岗位id
     * @return void
     */
    @SystemLog(title = "删除岗位",businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> deleteById(@PathVariable("id") Long id) {
        sysPostService.deleteById(id);
        return success();
    }

    /**
     * 新增岗位
     *
     * @param sysPostDto 请求参数
     * @return Long
     */
    @SystemLog(title = "新增岗位",businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> create(@RequestBody @Validated SysPostDto sysPostDto) {
        Long id = sysPostService.create(sysPostDto);
        return success(HttpStatus.CREATED,id);
    }

    /**
     * 根据id修改岗位
     *
     * @param id         岗位id
     * @param sysPostDto 请求参数
     * @return void
     */
    @SystemLog(title = "修改岗位",businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public R<Void> updateById(@PathVariable("id") Long id, @RequestBody @Validated SysPostDto sysPostDto) {
        sysPostService.updateById(id, sysPostDto);
        return success();
    }
}
